﻿using System;
using System.Collections.Generic;
using System.Timers;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game_Engine
{
    public class UpdatingText : Label
    {
        public StringBuilder DisplayText { get; set; }
        public int TextIndex { get; set; }
        public Timer Timer { get; set; }
        public int CharacterLimit { get; set; }
        public int LineLimit { get; private set; }
        public Queue<String> Lines { get; set; }

        public UpdatingText(Texture2D texture, Rectangle rectangle, Color textColor, int charLimit, int lineLimit)
        {
            Texture = texture;
            Bounds = rectangle; //It is important that the bounds are a function of the LineLimit and CharacterLimit - but figure out later
            TextColor = textColor;
            Selected = true;

            Timer = new Timer(200);     //500 milliseconds is half a second
            Timer.Elapsed += IntervalElapsed;

            DisplayText = new StringBuilder();
            FillText();
            TextIndex = 0;
            CharacterLimit = charLimit;
            LineLimit = lineLimit;

            Lines = new Queue<String>();
        }

        public override void Update()
        { 
            //New Queue plan. We Render the Queue as is.
            //There is a separate StringBuilder at the bottom of the text area which is animated by
            //the Timer event. It is rendered just below the queue. When the time is right, it is added
            //to the queue and the StringBuilder resets. (DisplayText)
            if (DisplayText.Length >= CharacterLimit)
            {
                Lines.Enqueue(DisplayText.ToString());
                //Console.WriteLine("New Line:");
                //Console.WriteLine(DisplayText);
                if (Lines.Count > LineLimit)
                    Lines.Dequeue();
                DisplayText.Clear();
            }
        }

        private void IntervalElapsed(object source, ElapsedEventArgs e)
        { 
            //At an interval we add some number of characters to the last string in the queue from Text
            DisplayText.Append(Text[TextIndex]);
            TextIndex++;
            if (TextIndex == Text.Length)
            {
                FillText();
                //Console.WriteLine("New Text:");
                //Console.WriteLine(Text);
                TextIndex = 0;
            }
        }

        public void FillText()
        {
            RandomTextGenerator rtg = new RandomTextGenerator();
            Text = rtg.generateString(rtg.Random.Next(10)+1);         //How many characters per line?
        }

        public override void Draw(SpriteBatch spriteBatch, SpriteFont font)
        {
            //base.Draw(spriteBatch, font);
            int i = Lines.Count;
            float lineHeight = Bounds.Height/LineLimit;
            spriteBatch.DrawString(font, DisplayText, new Vector2(Bounds.X, Bounds.Y + Bounds.Height),
                    TextColor, 0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0);
            foreach (String line in Lines)
            {
                spriteBatch.DrawString(font, line, new Vector2(Bounds.X, Bounds.Y + (Bounds.Height - (i * lineHeight))), 
                    TextColor, 0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0);
                i--;
            }
        }
    }
}
